package com.fx.device.service.activit.impl;

import com.fx.device.common.security.MyUserDetailsService;
import com.fx.device.common.util.ErrorEnum;
import com.fx.device.common.util.ReturnMessage;
import com.fx.device.common.util.ReturnMessageUtils;
import com.fx.device.repository.ProUserRepository;
import com.fx.device.service.activit.ProcessUserService;
import com.fx.device.vo.activit.ProUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;




@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProcessUserServiceImpl implements ProcessUserService {

    final private ProUserRepository proUserRepository;
    final private MyUserDetailsService myUserDetailsService;


    @Override
    public ReturnMessage<Object> getUsers() {
        try {


            List<HashMap<String, Object>> userList = proUserRepository.selectUser();


            return ReturnMessageUtils.success(userList);


        } catch (Exception e) {
            return ReturnMessageUtils.error(ErrorEnum.E_ACTIVIT_USER_GETLIST.getErrorCode(),
                    ErrorEnum.E_ACTIVIT_USER_GETLIST.getErrorMsg(), e.toString());
        }

    }

    @Override
    public ReturnMessage<Object> saveUser(ProUser proUser) {
        //设置主键的值

        //String password = new Md5Hash("123456",user.getMobile(),3).toString();

        try {
            String password = myUserDetailsService.passwordEncoder().encode(proUser.getPassword());
            //userInfoBean.setUsername("username");
            proUser.setPassword(password);//设置初始密码
            //userInfoBean.setRoles("ROLE_ACTIVITI_USER,GROUP_activitiTeam");
            //userInfoBean.setName("name");

            //调用dao保存部门
            proUserRepository.insert(proUser);
            return ReturnMessageUtils.success();
        } catch (Exception e) {

            return ReturnMessageUtils.error(ErrorEnum.E_ACTIVIT_USER_ADDUSER.getErrorCode(),
                    ErrorEnum.E_ACTIVIT_USER_ADDUSER.getErrorMsg(), e.toString());
        }



    }
}